/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.entity;

import com.velocityme.enums.ChangeDeltaType;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author rcrida
 */
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@NamedQueries( {
    @NamedQuery(name="findAllUndeleted",
                query="SELECT n FROM Node n WHERE n.deleted = FALSE"),
/*    @NamedQuery(name="findSystemNodeByName",
                query="SELECT n FROM Node n WHERE n.name = :name AND o.finalType = 'SystemNode'"),*/
/*    @NamedQuery(name="findDirectoryNode",
                query="SELECT OBJECT(o) FROM Node o WHERE o.type = 'Node' AND o.finalType = 'Directory'"),*/
    @NamedQuery(name="findOrderedChildren",
                query="SELECT n FROM Node n WHERE n.parentNode = :node AND n.deleted = FALSE ORDER BY n.name"),
    @NamedQuery(name="findSubLevel",
                query="SELECT DISTINCT c FROM SearchLevel s, Node c, IN (s.nodes) n WHERE s = :searchLevel AND c.parentNode = n AND c.deleted = FALSE"),
    @NamedQuery(name="findSearchNodes",
                query="SELECT DISTINCT n FROM Search s, IN (s.searchLevels) l, IN (l.nodes) n WHERE s = :search AND n.deleted = FALSE")
})
public class Node implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private static final long serialVersionUID = 1L;

    private boolean deleted;

    private String name;

    private String description;

    @ManyToOne
    private Node parentNode;
    
    public Node() {
    }

    public Node(Node parentNode, String name, String description) {
        this.parentNode = parentNode;
        this.name = name;
        this.description = description;
        deleted = false;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Node getParentNode() {
        return parentNode;
    }

    public void setParentNode(Node parentNode) {
        this.parentNode = parentNode;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public ChangeDelta addChangeDelta(ChangeDeltaType type, User user) {
        ChangeDelta changeDelta = new ChangeDelta(type, user, this);
        return changeDelta;
    }

    public String getPathName() {
        List<String> nodeNames = new ArrayList<String>();
        nodeNames.add(getName());
        Node pNode = getParentNode();
        while(pNode != null) {
            nodeNames.add(0, pNode.getName());
            pNode = pNode.getParentNode();
        }
        return StringUtils.join(nodeNames, '/');
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Node)) {
            return false;
        }
        Node other = (Node) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getName();
    }
}
